package com.jkgj.skykingkong.tms.application.dto;

import lombok.Data;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;

/**
 * @author Xuegui Yuan
 */
@Data
public class InvokeResult implements Serializable {
  public static final boolean SUCCESS = true;
  public static final boolean FAULT = false;
  private static final long serialVersionUID = 1L;
  private boolean success;
  private String message;
  private String errorStack;
  private Object data;

  public InvokeResult() {
    this.success = true;
  }

  public InvokeResult(String message) {
    this.success = true;
    this.message = message;
  }

  public InvokeResult(Object data) {
    this.success = true;
    this.data = data;
  }

  public InvokeResult(boolean success) {
    this.success = success;
  }

  public InvokeResult(boolean success, String message) {
    this.success = success;
    this.message = message;
  }

  public InvokeResult(Throwable exceptionMessage) {
    exceptionMessage.printStackTrace(new PrintWriter(new StringWriter()));
    this.success = false;
    this.message = exceptionMessage.getMessage();
  }

  public InvokeResult(Throwable exceptionMessage, boolean detailMsg) {
    exceptionMessage.printStackTrace(new PrintWriter(new StringWriter()));
    this.success = false;
    this.message = exceptionMessage.getMessage();
    if (detailMsg) {
      this.errorStack = exceptionMessage.toString();
    }
  }

  public static InvokeResult successResult() {
    return new InvokeResult(SUCCESS);
  }

  public static InvokeResult errorResult(String message) {
    return new InvokeResult(FAULT, message);
  }

}
